// index.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

// 引入SDK核心类，js文件根据自己业务，位置可自行放置
var QQMapWX = require('../../qqmap-wx-jssdk.min');
// 实例化API核心类
var qqmapsdk = new QQMapWX({
  // 申请的腾讯地图key
  key: 'KZ7BZ-DUGEZ-BQCXX-TQTRH-WXKY6-NMBPA'
});
// 导入请求方法
import api from '../../componets/api';

Page({
  data: {
    city: '', //市
    qv: '', //区
    weather: {}, //天气
    air: {}, //空气质量
    tip: '', //小提示
    week:[1,2,3,4,5,6,7],//星期
    icon:'',//小图标路径
    latitude:'',
    longitude:'',
  },
  onLoad() {
    this.getLocationQqmapsdk();
  },
  // 获取位置
  getLocationQqmapsdk:function() {
    // 获取经纬度
    wx.getLocation({
      type: "gcj02",
      success: (res) => {
        // console.log(res);//经纬度
        this.setData({
          latitude:res.latitude,
          longitude:res.longitude
        })
      
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: this.data.latitude, //纬度
            longitude: this.data.longitude //经度
          },
          success: (res1) => {
            // console.log(res1.result.address_component);//位置信息
            let {
              city,
              district
            } = res1.result.address_component;
            this.setData({
              city: city,
              qv: district
            })
            // 获取天气
            api.get(`https://api.vvhan.com/api/weather`, {city: city,type:'week'})
              .then(res => {
                if (res.statusCode == 200) {
                  
                  for(let i=0;i<res.data.data.length;i++){
                    if(res.data.data[i].type.includes('晴')){res.data.data[i].icon='/icon/qing.png'}
                    else if(res.data.data[i].type=='雨夹雪'){res.data.data[i].icon='/icon/yujiaxue.png'}
                    else if(res.data.data[i].type=='雷阵雨'){res.data.data[i].icon='/icon/leizhengyu.png'}
                    else if(res.data.data[i].type.includes('冰')){res.data.data[i].icon='/icon/bing.png'}
                    else if(res.data.data[i].type.includes('云')){res.data.data[i].icon='/icon/yun.jpg'}
                    else  if(res.data.data[i].type.includes('阴')){res.data.data[i].icon='/icon/yin.png'}
                    else if(res.data.data[i].type.includes('雷')){res.data.data[i].icon='/icon/lei.png'}
                    else if(res.data.data[i].type.includes('雨')){res.data.data[i].icon='/icon/yu.png'}
                    else if(res.data.data[i].type.includes('雪')){res.data.data[i].icon='/icon/xue.png'}
                    else if(res.data.data[i].type.includes('沙')){res.data.data[i].icon='/icon/sha.png'}
                    else if(res.data.data[i].type.includes('雾')){res.data.data[i].icon='/icon/wu.png'}
                    else if(res.data.data[i].type.includes('风')){res.data.data[i].icon='/icon/feng.png'}
                    else{this.setData({icon:''})}
                  }
                  // console.log(res.data);
                  //赋值
                  this.setData({
                    weather: res.data.data[0],
                    air: res.data.air,
                    tip: res.data.tip,
                    week:res.data.data
                  })
                  // 切换图标
                  if(res.data.data[0].type.includes('晴')){this.setData({icon:'/icon/qing.png'})}
                  else if(res.data.data[0].type=='雨夹雪'){this.setData({icon:'/icon/yujiaxue.png'})}
                  else if(res.data.data[0].type=='雷阵雨'){this.setData({icon:'/icon/leizhengyu.png'})}
                  else if(res.data.data[0].type.includes('冰')){this.setData({icon:'/icon/bing.png'})}
                  else if(res.data.data[0].type.includes('云')){this.setData({icon:'/icon/yun.jpg'})}
                  else  if(res.data.data[0].type.includes('阴')){this.setData({icon:'/icon/yin.png'})}
                  else if(res.data.data[0].type.includes('雷')){this.setData({icon:'/icon/lei.png'})}
                  else if(res.data.data[0].type.includes('雨')){this.setData({icon:'/icon/yu.png'})}
                  else if(res.data.data[0].type.includes('雪')){this.setData({icon:'/icon/xue.png'})}
                  else if(res.data.data[0].type.includes('沙')){this.setData({icon:'/icon/sha.png'})}
                  else if(res.data.data[0].type.includes('雾')){this.setData({icon:'/icon/wu.png'})}
                  else if(res.data.data[0].type.includes('风')){this.setData({icon:'/icon/feng.png'})}
                  else{this.setData({icon:''})}
                  
                } else {
                  console.log(res.errMsg);
                }
              })
          },
          fail: function (res) {
            console.log(res.message);
          }
        })
      },
      fail(err) {

      }
    });
  },
  // 选择地点
  select:function(){
    wx.chooseLocation({
      success: (res)=>{
       this.setData({
        latitude:res.latitude,
        longitude:res.longitude
       })
       qqmapsdk.reverseGeocoder({
        location: {
          latitude: this.data.latitude, //纬度
          longitude: this.data.longitude //经度
        },
        success: (res1) => {
          // console.log(res1.result.address_component);//位置信息
          let {
            city,
            district
          } = res1.result.address_component;
          this.setData({
            city: city,
            qv: district
          })
          // 获取天气
          api.get(`https://api.vvhan.com/api/weather`, {city: city,type:'week'})
            .then(res => {
              if (res.statusCode == 200) {
                
                for(let i=0;i<res.data.data.length;i++){
                  if(res.data.data[i].type.includes('晴')){res.data.data[i].icon='/icon/qing.png'}
                  else if(res.data.data[i].type=='雨夹雪'){res.data.data[i].icon='/icon/yujiaxue.png'}
                  else if(res.data.data[i].type=='雷阵雨'){res.data.data[i].icon='/icon/leizhengyu.png'}
                  else if(res.data.data[i].type.includes('冰')){res.data.data[i].icon='/icon/bing.png'}
                  else if(res.data.data[i].type.includes('云')){res.data.data[i].icon='/icon/yun.jpg'}
                  else  if(res.data.data[i].type.includes('阴')){res.data.data[i].icon='/icon/yin.png'}
                  else if(res.data.data[i].type.includes('雷')){res.data.data[i].icon='/icon/lei.png'}
                  else if(res.data.data[i].type.includes('雨')){res.data.data[i].icon='/icon/yu.png'}
                  else if(res.data.data[i].type.includes('雪')){res.data.data[i].icon='/icon/xue.png'}
                  else if(res.data.data[i].type.includes('沙')){res.data.data[i].icon='/icon/sha.png'}
                  else if(res.data.data[i].type.includes('雾')){res.data.data[i].icon='/icon/wu.png'}
                  else if(res.data.data[i].type.includes('风')){res.data.data[i].icon='/icon/feng.png'}
                  else{this.setData({icon:''})}
                }
                // console.log(res.data);
                //赋值
                this.setData({
                  weather: res.data.data[0],
                  air: res.data.air,
                  tip: res.data.tip,
                  week:res.data.data
                })
                // 切换图标
                if(res.data.data[0].type.includes('晴')){this.setData({icon:'/icon/qing.png'})}
                else if(res.data.data[0].type=='雨夹雪'){this.setData({icon:'/icon/yujiaxue.png'})}
                else if(res.data.data[0].type=='雷阵雨'){this.setData({icon:'/icon/leizhengyu.png'})}
                else if(res.data.data[0].type.includes('冰')){this.setData({icon:'/icon/bing.png'})}
                else if(res.data.data[0].type.includes('云')){this.setData({icon:'/icon/yun.jpg'})}
                else  if(res.data.data[0].type.includes('阴')){this.setData({icon:'/icon/yin.png'})}
                else if(res.data.data[0].type.includes('雷')){this.setData({icon:'/icon/lei.png'})}
                else if(res.data.data[0].type.includes('雨')){this.setData({icon:'/icon/yu.png'})}
                else if(res.data.data[0].type.includes('雪')){this.setData({icon:'/icon/xue.png'})}
                else if(res.data.data[0].type.includes('沙')){this.setData({icon:'/icon/sha.png'})}
                else if(res.data.data[0].type.includes('雾')){this.setData({icon:'/icon/wu.png'})}
                else if(res.data.data[0].type.includes('风')){this.setData({icon:'/icon/feng.png'})}
                else{this.setData({icon:''})}
                
              } else {
                console.log(res.errMsg);
              }
            })
        },
        fail: function (res) {
          console.log(res.message);
        }
      })
      },
    })
  }
 

})